<script setup lang="ts">
import { ref, onMounted } from 'vue'
import useRole from '@/hooks/acl/useRole'
import { addRoleAcl } from '@/axios/api/acl/role'
let formRef: any = ref('formRef') // 抽屉表单对象
const {
    tableData,
    roleadd_data,
    dialog,
    username,
    currentPage4,
    pageSize4,
    total4,
    rules,
    acldrawer,
    handleSizeChange,
    handleCurrentChange,
    handleSearch,
    handleReset,
    handleRole,
    handleEdit,
    handleDelete,
    handleAdd,
    usegetRoles,
    handleAddsubmit,
    menuArr,
    selectArr,
    defaultProps,
    role_id
} = useRole()

onMounted(() => {
    usegetRoles();
})
// 提交用户
async function handleUser() {
    await formRef.value.validate();
    handleAddsubmit();
}

let tree = ref<any>() // 树形控件对象



async function aclhandler() {
    //选中节点的ID
    let arr = tree.value.getCheckedKeys();
    //半选的ID
    let arr1 = tree.value.getHalfCheckedKeys();
    let arrs=arr.concat(arr1);
    // 删除0
    arrs.forEach(function(item: any, index: number) {
        if (item == 0) {
            arrs.splice(index, 1);
        }
    });
    let res = await addRoleAcl({
        role_id: role_id.value,
        acls: arrs
    })
    if (res.code == 200) {
        acldrawer.value = false;
    } else {
        alert(res.msg)
    }
}

</script>



<template>
    <el-card>
        <el-form class="form">
            <el-form-item label="角色名:">
                <el-input v-model="username" placeholder="请输入角色名"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleSearch">搜索</el-button>
                <el-button type="primary" @click="handleReset">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <el-card>
        <template #header>
            <el-button type="primary" @click="handleAdd">添加角色</el-button>
        </template>
        <el-scrollbar style="height: calc(100vh - 300px);"> <!-- 菜单滚动条 -->
            <el-table :data="tableData" style="width: 100%">
                <el-table-column type="selection" align="center"></el-table-column>
                <el-table-column type="index" label="序号"></el-table-column>
                <el-table-column prop="role_id" label="角色ID"></el-table-column>
                <el-table-column prop="role_name" label="角色名称"></el-table-column>
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button size="small" type="success" @click="handleRole(scope.row)">分配权限</el-button>
                        <el-button size="small" type="primary" @click="handleEdit()">编辑</el-button>
                        <el-popconfirm width="220" confirm-button-text="OK" cancel-button-text="No, Thanks"
                            icon="InfoFilled" icon-color="#626AEF" title="是否确认删除该角色?"
                            @confirm="handleDelete(scope.row.UserID)">
                            <template #reference>
                                <el-button type="danger" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </el-scrollbar>
        <footer style="margin: 10px 0;">
            <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 20, 30, 40, 50]"
                background layout="total, sizes, prev, pager, next, jumper" :total="total4"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </footer>
    </el-card>

    <!-- 抽屉 添加/编辑用户 -->
    <el-drawer ref="drawerRef" v-model="dialog" :title="roleadd_data.id ? '编辑角色' : '添加角色'" class="demo-drawer">
        <el-form ref="formRef" :model="roleadd_data" :rules="rules" label-width="80px">
            <el-form-item label="角色id" prop="username" v-if="roleadd_data.id">
                <el-input v-model="roleadd_data.id" placeholder="请输入角色id"></el-input>
            </el-form-item>
            <el-form-item label="角色名称" prop="rolename">
                <el-input v-model="roleadd_data.rolename" placeholder="请输入角色名称"></el-input>
            </el-form-item>

            <el-form-item>
                <el-popconfirm width="220" confirm-button-text="OK" cancel-button-text="No, Thanks" icon="InfoFilled"
                    icon-color="#626AEF" title="是否确认添加该角色?" @confirm="handleUser">
                    <template #reference>
                        <el-button type="primary">提交</el-button>
                    </template>
                </el-popconfirm>

                <el-button type="danger">取消</el-button>
            </el-form-item>
        </el-form>
    </el-drawer>

    <!-- 抽屉组件:分配职位的菜单权限与按钮的权限 -->
    <el-drawer v-model="acldrawer">
        <template #header>
            <h4>分配菜单与按钮的权限</h4>
        </template>
        <template #default>
            <!-- 树形控件 -->
            <el-tree ref="tree" :data="menuArr" show-checkbox node-key="acl_id" default-expand-all
                :default-checked-keys="selectArr" :props="defaultProps" check-strictly/>
        </template>
        <template #footer>
            <div style="flex: auto">
                <el-button @click="acldrawer = false">取消</el-button>
                <el-popconfirm width="220" confirm-button-text="OK" cancel-button-text="No, Thanks" icon="InfoFilled"
                    icon-color="#626AEF" title="是否确认添加该角色的权限?" @confirm="aclhandler">
                    <template #reference>
                        <el-button type="primary">提交</el-button>
                    </template>
                </el-popconfirm>
            </div>
        </template>
    </el-drawer>
</template>


<style scoped lang="scss">
.form {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
</style>